package org.abettor.pushbox.view;

import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.map.Point;
import org.abettor.pushbox.map.Step;
import org.abettor.pushbox.util.Statck;
import org.abettor.pushbox.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.Toast;

public class PushBoxView extends BoxView {
	private static final float maxscale = 1.25f;
	private Statck<Step> stackMap = new Statck<Step>();// 记录操作步骤的对战
	private static final int MAX_REDO = 3;// 允许的用户反悔步骤
	private int canUsedRedoStep = 0;// 剩下的最多反悔的步骤
	private Boxmap origenMap;// 记录刚开始的图
	private boolean hasCompleted = false;//设置是否已经完成
	public PushBoxView(Context context) {
		super(context);
	}

	// 记录历史记录
	private void recordHistory(Step step) {
		stackMap.push(step);
		canUsedRedoStep = canUsedRedoStep + 1 > MAX_REDO ? MAX_REDO
				: canUsedRedoStep + 1;
	}

	/**
	 * 返回上一步的操作
	 */
	public void revertStep() {
		if(hasCompleted)
			return;
		if (stackMap.size() == 0) {
			// Do not have any record,do noting
			return;
		}
		if (canUsedRedoStep <= 0) {
			Toast.makeText(this.getContext(),
					this.getContext().getText(R.string.revert_step_beyond_max),
					Toast.LENGTH_SHORT).show();
			return;
		}
		// 初始时人的坐标
		int x = map.getPerson().getX();
		int y = map.getPerson().getY();
		// 从堆栈栈中弹出记录
		Step step1 = stackMap.pop();
		Step step2 = stackMap.peek();
		map.revertPreStep(step1, step2);
		// 弹出后人的坐标
		int x1 = map.getPerson().getX();
		int y1 = map.getPerson().getY();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (x == x1) {
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x, y + 1);
			drawEveryClip(c1, x, y - 1);
		} else if (y == y1) {
			drawEveryClip(c1, x - 1, y);
			drawEveryClip(c1, x + 1, y);
			drawEveryClip(c1, x, y);
		}
		canUsedRedoStep--;
		super.setImageBitmap(mPushBoxImage);
	}

	/**
	 * 重新开始的操作
	 */
	public void restartPush() {
		if(hasCompleted)
			return;
		if (stackMap.size() == 0) {
			// The stack map already empty,so do noting
			return;
		}
		map = Boxmap.copy(origenMap);
		stackMap.clear();
		canUsedRedoStep = 0;
		super.generateBitmap();
	}

	public PushBoxView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public void setMap(Boxmap map) {
		super.setMap(map);
		origenMap = Boxmap.copy(map);// 拷贝一份
	}

	/**
	 * 进行放大的操作
	 */
	public void zoomIn() {
		super.zoomIn(maxscale);
	}

	/**
	 * 进行缩小的操作
	 */
	public void zoomOut() {
		super.zoomOut(maxscale);
	}

	/**
	 * 进行up的操作
	 */
	public void up() {
		if(hasCompleted)
			return;
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean ispushbox = map.isBox(x, y - 1);
		boolean flag = map.up();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			recordHistory(new Step(Step.UP, ispushbox));
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x, y - 1);
			drawEveryClip(c1, x, y - 2);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	/**
	 * 进行left的操作
	 */
	public void left() {
		if(hasCompleted)
			return;
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean ispushbox = map.isBox(x - 1, y);
		boolean flag = map.left();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			recordHistory(new Step(Step.LEFT, ispushbox));
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x - 1, y);
			drawEveryClip(c1, x - 2, y);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	/**
	 * 进行right的操作
	 */
	public void right() {
		if(hasCompleted)
			return;
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean ispushbox = map.isBox(x + 1, y);
		boolean flag = map.right();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			recordHistory(new Step(Step.RIGHT, ispushbox));
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x + 1, y);
			drawEveryClip(c1, x + 2, y);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	/**
	 * 进行down的操作
	 */
	public void down() {
		if(hasCompleted)
			return;
		Point point = map.getPerson();
		int x = point.getX();
		int y = point.getY();
		boolean ispushbox = map.isBox(x, y + 1);
		boolean flag = map.down();
		Canvas c1 = new Canvas(mPushBoxImage);
		if (!flag) {// 没有成功,绘制一个节点及可
			drawEveryClip(c1, x, y);
		} else {// 成功绘制三个节点
			recordHistory(new Step(Step.DOWN, ispushbox));
			drawEveryClip(c1, x, y);
			drawEveryClip(c1, x, y + 1);
			drawEveryClip(c1, x, y + 2);
		}
		super.setImageBitmap(mPushBoxImage);
	}

	/**
	 * 滚动的起始点是否在人的位置上
	 * 
	 * @return
	 */
	public boolean isScrollStartPointOnPerson(float x, float y) {
		if (super.mPushBoxImage == null) {
			return false;
		}
		Matrix m = getImageViewMatrix();
		float[] topLeft = new float[] { 0, 0 };
		float[] botRight = new float[] { mPushBoxImage.getWidth(),
				mPushBoxImage.getHeight() };
		translatePoint(m, topLeft);
		translatePoint(m, botRight);
		float height = botRight[1] - topLeft[1];
		float width = botRight[0] - topLeft[0];
		float clipx = width / super.map.getWidth();
		float clipy = height / super.map.getHeight();
		int map_x = (int) ((x - topLeft[0]) / clipx);
		int map_y = (int) ((y - topLeft[1]) / clipy);
		// 或操作,灵活些
		if (map_x == map.getPerson().getX() || map_y == map.getPerson().getY()) {
			return true;
		}
		return false;
	}
	/**
	 * 判断地图是否成功
	 * @return
	 */
	public boolean checkIsSuccess(){
		return map.isSuccess();
	}
	private void translatePoint(Matrix matrix, float[] xy) {
		matrix.mapPoints(xy);
	}

	public Statck<Step> getStackMap() {
		return stackMap;
	}

	public void setHasCompleted(boolean hasCompleted) {
		this.hasCompleted = hasCompleted;
	}
}
